package com.wt.struggle.goodbye.user.activity

import android.content.Intent
import android.os.Bundle
import android.support.design.widget.TabLayout
import android.support.v4.app.Fragment
import android.support.v4.app.FragmentStatePagerAdapter
import android.support.v7.widget.GridLayoutManager
import android.support.v7.widget.RecyclerView
import android.support.v7.widget.StaggeredGridLayoutManager
import android.view.View
import com.wt.struggle.goodbye.R
import com.wt.struggle.goodbye.base.*
import com.wt.struggle.goodbye.info.NetInfo
import com.wt.struggle.goodbye.info.ProUserInfo
import com.wt.struggle.goodbye.main.fragment.MainFragment2
import com.wt.struggle.goodbye.user.CollectFragment
import com.xin.lv.yang.utils.utils.DataUtil
import com.xin.lv.yang.utils.utils.ImageUtil
import kotlinx.android.synthetic.main.head.*
import kotlinx.android.synthetic.main.user_information_layout.*

/**
 * 用户详情界面
 */
class UserInfoActivity : ProActivity() {

    override fun handler(what: Int, arg1: Int, netInfo: NetInfo) {

        when (what) {

            Send.ATTENTION_CODE -> {

                removeLoadDialog()

                if (netInfo.code == Config.SUCCESS) {
                    buttonFoucs.text = "已关注"
                }

                showToastShort(netInfo.message!!)

            }

            Send.GET_USER_INFO -> {

                if (netInfo.code == Config.SUCCESS) {
                    val result = netInfo.dataResult
                    if (result != null && result != "" && result != "null") {
                        val userInfo: ProUserInfo = gson!!.fromJson(result, ProUserInfo::class.java)

                        showUserInfo(userInfo)

                    }
                }
            }
        }
    }

    var userInfo: ProUserInfo? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.user_information_layout)

        userInfo = intent.getParcelableExtra<ProUserInfo>("user")

        imageBack.setOnClickListener {
            finish()
        }

        tvTitle.text = userInfo!!.nickName

        getUserInfo(userInfo!!)

        initUserViewPager()

        buttonFoucs.setOnClickListener {
            send!!.attentionUser(userInfo!!, handler)
            showLoadDialog("关注中")

        }

        linearSendMessage.setOnClickListener {
            val intent = Intent(this, SiXinOrRecoverActivity::class.java)
            intent.putExtra("code", 1)
            intent.putExtra("user", userInfo)
            startActivity(intent)
        }

    }

    /**
     * 获取用户信息
     */
    private fun getUserInfo(userInfo: ProUserInfo) {

        val uid = userInfo.appUserId!!.toInt()

        log("----------$uid")

        send!!.getMyUserInfo(uid, handler)
    }

    val fragmentList = arrayListOf<ProV4Fragment>()

    private fun initUserViewPager() {

        val fragment1 = MainFragment2()
        fragment1.code = 2
        fragment1.userInfo = userInfo

        fragmentList.add(fragment1)

        fragmentList.add(CollectFragment())

        userViewPager.adapter = object : FragmentStatePagerAdapter(supportFragmentManager) {
            override fun getItem(position: Int): Fragment = fragmentList[position]

            override fun getCount(): Int = fragmentList.size

            override fun getPageTitle(position: Int): CharSequence? {
                return when (position) {
                    0 -> "帖子"
                    1 -> "收藏"
                    else -> ""
                }
            }

        }

        userViewPager.addOnPageChangeListener(object : TabLayout.TabLayoutOnPageChangeListener(userTabLayout) {})

        userTabLayout.setupWithViewPager(userViewPager)
        val ww = resources.getDimension(R.dimen.dp_12).toInt()

        userTabLayout.post {
            setIndicator(userTabLayout, ww, ww)
        }

        userTabLayout.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
            override fun onTabReselected(tab: TabLayout.Tab?) {

            }

            override fun onTabUnselected(tab: TabLayout.Tab?) {
            }

            override fun onTabSelected(tab: TabLayout.Tab?) {
                val position = tab!!.position

                when (position) {
                    0 -> {
                        val ff = fragmentList[position] as MainFragment2
                        ff.code = 2
                        ff.userInfo = userInfo
                        userViewPager.setCurrentItem(position, true)
                    }
                    1 -> {
                        userViewPager.setCurrentItem(position, true)
                    }
                }

            }

        })

    }


    private fun showUserInfo(userInfo: ProUserInfo?) {

        ImageUtil.getInstance().loadCircleImage(this, userHead, Config.GET_IMAGE_URL + userInfo!!.avatar, 0)

        tvCollectNum.text = (userInfo.numberOfContentCollected!!).toString()
        tvFouceNum.text = userInfo.numberOfCollected.toString()
        tvFansNum.text = userInfo.numberOfFans.toString()
        tvCycleNum.text = userInfo.numberOfCircleCollected.toString()

        val location = Share.getLocation(this)
        if (userInfo.lat != "" && userInfo.lng != "") {
            val nnn =
                DataUtil.gps2m(
                    location.latitude,
                    location.longitude,
                    userInfo.lat!!.toDouble(),
                    userInfo.lng!!.toDouble()
                )

            if (nnn > 1) {
                tvIndexAddress.text = "距离 >1km"
            } else {
                tvIndexAddress.text = "距离 <1km"
            }
        } else {
            tvIndexAddress.text = ""
        }

        tvUserSignName.visibility = View.GONE

        if (userInfo.gender == "0") {
            userSex.setImageResource(R.drawable.wblive_datacard_ic_male)
        } else {
            userSex.setImageResource(R.drawable.wblive_datacard_ic_female)
        }


    }
}